#include "ti_msp_dl_config.h"
#include "delay.h"
#include "Motor.h"
#include "key.h"
#include "LED.h"
#include "GAY_GET.h"
#include "OLED.h"
int uart_data = 0;

void uart0_send_char(char ch); //串口0发送单个字符
uint8_t quan = 0;

void uart0_send_string(char* str); //串口0发送字符串
float kp=200,ki,kd=0;
int time=1300,time2 =400;
int speed_l,speed_r,keyum,speed_base = 2000,new1,new2,flag2 = 0,turn=0,N=0;
int main(void)
{
 
    SYSCFG_DL_init();
    NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN);
    //使能串口中断
    NVIC_EnableIRQ(UART_0_INST_INT_IRQN);
    uart0_send_string("uart0 start work\r\n");
    LED_ON();
    while (1)
    {  
         uint8_t gay = get_gay();
        keyum = Key_Scan();
        if (keyum == 2) {
            N++;
            LED1_ON();
            delay_ms(50);
        }
        else {
            LED1_OFF();
        }

        
        if (keyum == 3) {
          flag2 = 1;
          LED2_ON();
    //      set_1motorspeed(2000);
    //      set_2motorspeed(1000);
    //        delay_ms(time2);
        }
    if (flag2 == 1) {
    
    
            if(turn == 0)
            {
            set_1motorspeed(2000);
            set_2motorspeed(500);  


               if (gay==0x77||gay == 0x47||gay == 0x73|| gay == 0x76 || gay == 0x67||gay == 0x7B)
                        {
                        turn = 1;
                        }  
            }      
    }
         uart_data = DL_UART_Main_receiveData(UART_0_INST);
          
         if (uart_data == 0x01) {
                LED1_ON();
                time+=100;
         }
         if (uart_data == 0x02) {
            LED1_ON();
                time-=100;
         }
         if (uart_data ==0x03) {
            LED1_ON();
             kp+=50;
         }
        if (uart_data == 0x04) {
            LED1_ON();
             kp-=50;
        }
         if (uart_data == 0x05) {
            LED1_ON();
            time2+=100;
        }    
        if (uart_data == 0x06) {
            LED1_ON();
             time2-=100;   
        }   
    if (N ==1 && quan == 5) {
    flag2 = 0;
    set_1motorspeed(0);
    set_2motorspeed(0);   
    
    }
    else if (N ==2 && quan == 9) {  
    flag2 = 0;
    set_1motorspeed(0);
    set_2motorspeed(0);   
    }
    else if (N ==3 && quan == 13) {  
    flag2 = 0;
    set_1motorspeed(0);
    set_2motorspeed(0);   
    }
    else if (N ==4 && quan == 17) {  
    flag2 = 0;
    set_1motorspeed(0);
    set_2motorspeed(0);   
    }
    else if (N ==5 && quan == 21) {  
    flag2 = 0;
    set_1motorspeed(0);
    set_2motorspeed(0);   
    } 

    if(flag2 == 1&&turn==1) {    
        pid_gary(gay); 
    }

    
     
    }
}

//串口发送单个字符
void uart0_send_char(char ch)
{
    //当串口0忙的时候等待，不忙的时候再发送传进来的字符
    while( DL_UART_isBusy(UART_0_INST) == true );
    //发送单个字符
    DL_UART_Main_transmitData(UART_0_INST, ch);
}
//串口发送字符串
void uart0_send_string(char* str)
{
    //当前字符串地址不在结尾 并且 字符串首地址不为空
    while(*str!=0&&str!=0)
    {
        //发送字符串首地址中的字符，并且在发送完成之后首地址自增
        uart0_send_char(*str++);
    }
}


void UART_0_INST_IRQHandler(void)
{
    //如果产生了串口中断
    switch( DL_UART_getPendingInterrupt(UART_0_INST) )
    {
        case DL_UART_IIDX_RX://如果是接收中断
            //将发送过来的数据保存在变量中
            uart_data = DL_UART_Main_receiveData(UART_0_INST);
            //将保存的数据再发送出去
            uart0_send_char(uart_data);
                DL_UART_Main_transmitData(UART0,new1);        uart0_send_string("new1:") ;       

             DL_UART_Main_transmitData(UART0,kp);     


            break;

        default://其他的串口中断
            break;
    }
}